package zhuoyue.com.yanjiaohui.acticity;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.google.gson.Gson;

import java.util.HashMap;
import java.util.Map;

import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.RequestBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import zhuoyue.com.yanjiaohui.R;
import zhuoyue.com.yanjiaohui.entity.PostRegistCallBackEntity;
import zhuoyue.com.yanjiaohui.entity.PostRegisterEntity;
import zhuoyue.com.yanjiaohui.entity.SmsBackEntity;
import zhuoyue.com.yanjiaohui.interfacepackage.INetService;
import zhuoyue.com.yanjiaohui.url.UrlConfig;
import zhuoyue.com.yanjiaohui.util.MD5util;
import zhuoyue.com.yanjiaohui.util.MyCountDownTimer;
import zhuoyue.com.yanjiaohui.util.NetWorkApi;

public class RegistActivity extends AppCompatActivity {

    private Button mCountButton;
    MyCountDownTimer mMyCountDownTimer;

    public static final String mobile=new String();
    private EditText mMobile, mCode, mUser_name, mUser_pwd;
    private String sMobile, sCode, sUser_name, sUser_pwd;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_regist);

        initView();

        initRegistData();

        mMyCountDownTimer = new MyCountDownTimer(9000, 100, mCountButton);

        // initRetrofit();


    }

    private void initRetrofit() {


    }

    private void initRegistData() {

        sUser_pwd = mUser_pwd.getText().toString();
        sUser_name = mUser_name.getText().toString();
        sMobile = mMobile.getText().toString();
        sCode = mCode.getText().toString();


    }

    private String getMd5OkData(String data) {
        String encrypt = MD5util.encrypt(data);
        String str = encrypt + "yanjiaohui".toString();
        String encrypt1 = MD5util.encrypt(str);
        return encrypt1;
    }

    private void initView() {

        mCode = (EditText) findViewById(R.id.code);
        mMobile = (EditText) findViewById(R.id.mobile);
        mUser_name = (EditText) findViewById(R.id.user_name);
        mUser_pwd = (EditText) findViewById(R.id.user_pwd);
        mCountButton = (Button) findViewById(R.id.count_btn);

    }

    //点击立即注册
    public void regist_now_click(View view) {

//        PostRegisterEntity postRegisterEntity = new PostRegisterEntity();
//        postRegisterEntity.user_name = "s123123";
//        postRegisterEntity.mobile = "13716222924";
//        postRegisterEntity.code = sCode;
//        postRegisterEntity.user_pwd = getMd5OkData("yanjiaohui123");
//        postRegisterEntity.mac_id = null;

        Map<String, String> map = new HashMap<>();
        map.put("mobile", "18345963106");
        map.put("user_name", "wasd5200");
        map.put("user_pwd", getMd5OkData(sUser_pwd));
        map.put("code", "123456");

        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(UrlConfig.BASEURL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        INetService iNetService = retrofit.create(INetService.class);

        iNetService.PostRegistData(map)
                .enqueue(new Callback<PostRegistCallBackEntity>() {
            @Override
            public void onResponse(Call<PostRegistCallBackEntity> call, Response<PostRegistCallBackEntity> response) {
                PostRegistCallBackEntity body = response.body();
                if (body != null) {
                    Toast.makeText(RegistActivity.this, "body" + body.toString(), Toast.LENGTH_SHORT).show();
                    Log.d("flag", "body back" + body.getBack());
                    Log.d("flag", "body info" + body.getInfo());
                    Log.d("flag", "body error" + body.getError());
                } else {
                    Toast.makeText(RegistActivity.this, "body error", Toast.LENGTH_SHORT).show();
                }
            }
            @Override
            public void onFailure(Call<PostRegistCallBackEntity> call, Throwable t) {

            }
        });
    }

    //获取验证码，倒计时功能
    public void get_num_click(View view) {
        mMyCountDownTimer.start();

        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(UrlConfig.BASEURL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        INetService iNetService = retrofit.create(INetService.class);

        iNetService.postphoneData().enqueue(new Callback<SmsBackEntity>() {
            @Override
            public void onResponse(Call<SmsBackEntity> call, Response<SmsBackEntity> response) {
                SmsBackEntity body = response.body();
                if (body != null) {
                    Toast.makeText(RegistActivity.this, "body" + body.getBack().toString(), Toast.LENGTH_SHORT).show();
                    Log.d("flag", "body" + body.getBack());
                    Log.d("flag", "body" + body.getInfo());
                } else {
                    Toast.makeText(RegistActivity.this, "body 为空", Toast.LENGTH_SHORT).show();
                }
            }
            @Override
            public void onFailure(Call<SmsBackEntity> call, Throwable t) {

            }
        });
    }

}
